package top.infopub.mgr.sg.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.cache.RedisCached;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sg.dao.DeskNoticeDao;
import top.infopub.mgr.sg.service.DeskNoticeService;
import top.infopub.mgr.sg.vo.DeskNoticeVo;
import top.infopub.model.sg.DeskNotice;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class DeskNoticeServiceImpl extends GenericServiceImpl<DeskNotice, String> implements
		DeskNoticeService {

	@Autowired
	private DeskNoticeDao deskNoticeDao;
	
	@Autowired
    private RedisCached redisCached;
	
	@Override
	public GenericDao<DeskNotice, String> getDao() {
		return deskNoticeDao;
	}

    @Override
    public Page<DeskNoticeVo> selectPageByDomainWhere(Page<DeskNoticeVo> page, DeskNoticeVo deskNoticeVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeskNoticeVo>)deskNoticeDao.selectByDomainWhere(deskNoticeVo);
    }

    @Override
    public void updateIsNoTop() {
        deskNoticeDao.updateIsNoTop();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNotice(DeskNotice deskNotice) {
        //如果新插入的公告置顶，则把之前置顶的数据更新为不置顶
        if(deskNotice.getIsTop() == 1){
            deskNoticeDao.updateIsNoTop();
        }
        // 执行新增
        if (StringUtils.isBlank(deskNotice.getId())) {
            deskNotice.setTypes(deskNotice.getTypes().trim());
            deskNotice.setSummary(deskNotice.getSummary().trim());
            deskNotice.setTitle(deskNotice.getTitle().trim());
            deskNotice.setState(1);// 默认是1=启用
            deskNotice.setAddBy(SessionUserUtils.getSessionUserId());
            deskNotice.setAddTime(new Date());
            deskNoticeDao.insert(deskNotice);
            
        }else {// 执行修改
            DeskNotice deskNoticeUpdate = deskNoticeDao.selectByPrimaryKey(deskNotice.getId());
            deskNoticeUpdate.setTypes(deskNotice.getTypes().trim());
            //标题
            deskNoticeUpdate.setTitle(deskNotice.getTitle().trim());
            //摘要
            deskNoticeUpdate.setSummary(deskNotice.getSummary().trim());
            //是否置顶
            deskNoticeUpdate.setIsTop(deskNotice.getIsTop());
            //内容
            deskNoticeUpdate.setContent(deskNotice.getContent());
            deskNoticeUpdate.setUptTime(new Date());
            deskNoticeUpdate.setUptBy(SessionUserUtils.getSessionUserId());
            this.updateBySelective(deskNoticeUpdate);
        }
    }

    @Override
    public Page<DeskNoticeVo> selectHomePageByDomainWhere(Page<DeskNoticeVo> page,
                                                        DeskNoticeVo deskNoticeVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<DeskNoticeVo> deskNotices = deskNoticeDao.selectByDomainWhere(deskNoticeVo);
        if(deskNotices.getResult().size() > 0){
            AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
            List<DeskNoticeVo> notices = deskNotices.getResult();
            for(DeskNoticeVo notice : notices){
                String key = notice.getId();
                String member = vo.getId();
                if(redisCached.sismember(key, member)){//判断用户是否已读
                    notice.setRead(true);
                }else{
                    notice.setRead(false);
                }
            }
        }
        return deskNotices;
    }
}